#include <cm/net/net_manager.h>

#include <stdio.h>
#include <string.h>

struct net_manager *manager = NULL;
struct net_session *session = NULL;

void client_proc(enum SESSION_EVENT e, struct net_session *s,
		struct cm_fifo *data)
{
	switch (e) {
	case SESSION_E_BROKEN:
		printf("session broken.\n");
		netm_closesession(manager, &session);
		break;
	case SESSION_E_DATA:
	{
		printf("data from server: ");
		char buffer;
		while (0 == cmfifo_getdata(data, &buffer, 1))
			printf("%c", buffer);

		printf("\n");
		break;
	}
	case SESSION_E_NEW:
		break;
	default:
		break;
	}
}

int main()
{
	manager = netm_create(NETM_O_NONE);
	if (manager == NULL) {
		printf("create manager failed.\n");
		return 0;
	}
	session = netm_addclient(manager, "127.0.0.1",
			12341, 512, 512, client_proc);
	if (session == NULL) {
		printf("create client failed.\n");
		netm_destroy(&manager);
		return 0;
	}

	char buffer[256];
	while (session != NULL && EOF != scanf("%s", buffer)) {
		session_send(session, buffer, strlen(buffer), 2000);
	}

	netm_destroy(&manager);
}
